<?php
//
//  VictoryCMS - Content managment system and framework.
//  Copyright (C) 2009  Lewis Gunsch
//
//  This file is part of VictoryCMS.
//  
//  VictoryCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  VictoryCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with VictoryCMS.  If not, see <http://www.gnu.org/licenses/>.

/**
 * TestVictoryCMS main startup. This is the entry point to the TestVictoryCMS
 * system. It initializes the test class autoloader.
 * 
 * <strong>Note:</strong> This depends on <strong>VcmsMain.php</strong> for
 * interacting with VictoryCMS. This files should be located in a test folder
 * outside VictoryCMS's include folder.
 *
 * @author Lewis Gunsch
 * @filesource
 * @package testing
 * @license http://www.gnu.org/licenses/gpl.html
 */

require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'simpletest'.DIRECTORY_SEPARATOR.'extensions'.DIRECTORY_SEPARATOR.'phpunit_test_case.php');
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'include'.DIRECTORY_SEPARATOR.'VcmsMain.php');

//overwrite Vcms's autoloader
spl_autoload_register('class_test_autoloader');
set_error_handler('class_test_errorhandler', E_STRICT);

/**
 * The main startup method for initializing TestVictoryCMS.
 * This is normally called from the index.php file, or default
 * start page.
 * @return void
 *
 * @todo Finish implementing the startup.
 */
function VcmsTestStartup() {
	// enable sessions here for other pages.
	session_start();

	$basedir = dirname(__FILE__).DIRECTORY_SEPARATOR;
	$modeldir = $basedir.'model_tests'.DIRECTORY_SEPARATOR;
	$list = scandir($modeldir);

	$groupTest = new GroupTest('VictoryCMS Test Suite');

	foreach ($list as $item) {
		$item = $modeldir.$item;
		if(is_file($item) && is_readable($item)) {
			$iteminfo = pathinfo($item);
			if($iteminfo['extension'] == 'php') {
				require_once($item);
				$groupTest->addTestCase(new $iteminfo['filename']);
			}
		}
	}
	$groupTest->run(new HtmlReporter());
}

/**
 *
 */
function class_test_errorhandler($errno, $errstr='', $errfile='', $errline='') {
	return false;
}

/**
 * An class autoloader for loading testing classes.
 * @return boolean true if class was loaded, and false if not.
 */
function class_test_autoloader($class) {

	$testingPath = FolderMap::getHomeDirPath().'test'.DIRECTORY_SEPARATOR;

	if (class_exists($class, false) || interface_exists($class, false)) {
		return true;
	}

	// try loading the class from the main class autoloader first.
	if (vcms_class_autoloader($class) == true)
		return true;

	if (file_exists($testingPath.$class.'.php')) {
		require_once($testingPath.$class.'.php');

	} else if (file_exists($testingPath.'model_tests'.DIRECTORY_SEPARATOR.$class.'.php')) {
		require_once($testingPath.'model_tests'.DIRECTORY_SEPARATOR.$class.'.php');

	} else {
		return false;
	}
	return true;
}
?>
